﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


using System.Data;
using System.Data.SqlClient;
using DAL;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
namespace EDMS.Admin.DelegateRecord
{
    public partial class RecordReturns : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //判断用户是否已经登录
                try
                {
                    if (!CMSUser.Current.IsLogin)
                    {
                        Response.Redirect("~/Login.aspx");
                    }
                }
                catch (Exception)
                {
                    Response.Redirect("~/Login.aspx");
                }
                databind();
            }
        }
        void databind()
        {
            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {

                string jlxh = Request.QueryString["id"].ToString();
                string CurrentXjbh = Session["Xjbh"].ToString();
                string strsql = "";
                if (CMSUser.Current.IsAdmin == true)
                {
                     strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl where jlbh='" + jlxh + "' order by fssj desc ";
                }
                else
                {
                     strsql = "select *,convert(varchar(30),fssj,111) as fstime,convert(varchar(30),wtsdysj,111) as wtsdytime from wtjl where xjbh='" + CurrentXjbh + "' and jlbh='" + jlxh + "' order by fssj desc ";
                }

                DataTable table = Frame.Global.GlobalSqlHelper.ExecuteSQLToDataTable(strsql);
                gvShow.DataSource = table;
                gvShow.KeyFieldName = "jlbh";
                gvShow.DataBind();

                // 文本框只读设置
                txtReadOnly();
                #region//将数据显示在文本框内
                var record = lqDB.Wtjl.FirstOrDefault(r => r.jlbh == jlxh);
                //根据选举编号查处选举名称 
                string xjbho = record.xjbh;
                var XMo = lqDB.Xjdj.FirstOrDefault(m => m.xjbh == xjbho);
                string xjmc = XMo.xjmc;

                txtjlbh.Text = jlxh;                    //记录编号
                //选举名称和编号
                cmdxjmc.DataSourceID = "";
                cmdxjmc.Items.Clear();
                ListEditItem list = new ListEditItem();
                list.Text = xjmc + "(编号：" + record.xjbh + ")";
                list.Value = record.xjbh;
                cmdxjmc.Items.Add(list);
                cmdxjmc.SelectedIndex = 1;
               
                txtwtname.Text = record.wtname;         //委托人姓名      *
                txtbwtname.Text = record.bwtname;       //被委托人姓名    *
                txtwtsf.Text = record.wtrsfzh;          //委托人身份证    *
                txtbwtsf.Text = record.bwtrsfzh;        //被委托人身份证   *
                txtip.Text = record.ip;                 //委托人电脑IP
                cmdprint.Text = record.wtsdysj.ToString();  //委托书打印时间
                lblyzm.Text = record.yzm;               //验证码
                txtfssj.Text = record.fssj.ToString();  //委托发生时间
                string wtfs = isWtfs(record.wtfs);
                txtwtfs.Text = wtfs;                      //委托方式
                string wtzt = isWtzt(record.flag);
                txtflag.Text = wtzt;                      //委托状态 
                #endregion

                //显示受理工作记录
                var Sljl = lqDB.Sljl.FirstOrDefault(s => s.wtjlbh == jlxh);
                if (Sljl != null)
                {
                    txtslczr.Text = CMSUser.Current.Name;
                    txtslsj.Text = Sljl.fssj.ToString();
                    txtslsm.Text = Sljl.sm;
                }

                if (record.ishf == '1')//判断是否已回访
                {
                    //进行回访操作
                    lblczr.Text = CMSUser.Current.Name;   //当前回访操作人
                    lblhfsj.Text = DateTime.Now.ToString();//当前回访工作日期
                    btnOK.Visible = true;

                }
                else
                {
                    btnOK.Visible = false;
                    //显示回访工作记录
                    var Hfjl = lqDB.Hfjl.FirstOrDefault(s => s.wtjlbh == jlxh);
                    if (Hfjl != null)
                    {
                        txtsm.Text = Hfjl.sm;
                        lblhfsj.Text = Hfjl.fssj.ToString();
                        lblczr.Text = Hfjl.czr;
                    }
                }

                if (btnOK.Text == "请先受理")
                {
                    btnOK.Visible = false;
                }


            }
        }
        //委托方式判断
        string isWtfs(string fs)
        {
            string wtfs = "";
            switch (fs)
            {
                case "D":
                    wtfs = "电话委托";
                    break;
                case "X":
                    wtfs = "短信委托";
                    break;
                case "W":
                    wtfs = "网站委托";
                    break;
                default:
                    break;
            }
            return wtfs;
        }
        //委托状态判断
        string isWtzt(char zt)
        {
            string wtzt = "";
            switch (zt)
            {
                case '9':
                    wtzt = "成功";
                    break;
                case '0':
                    wtzt = "失败";
                    break;
                case '1':
                    wtzt = "待定";
                    break;
            }
            return wtzt;
        }
        //文本框只读设置
        void txtReadOnly()
        {
            #region 文本框只读设置
           // txtxjbh.ReadOnly = true;
            txtjlbh.ReadOnly = true;
            txtwtfs.ReadOnly = true;
            txtflag.ReadOnly = true;
            txtip.ReadOnly = true;
           // cmdprint.ReadOnly = true;
            lblyzm.ReadOnly = true;
            txtfssj.ReadOnly = true;
            txtslsm.ReadOnly = true;
            txtslczr.ReadOnly = true;
            txtslsj.ReadOnly = true;
            lblczr.ReadOnly = true;
            txtfssj.ReadOnly = true;
            lblhfsj.ReadOnly = true;
            #endregion
        }
        //插入回访记录
        void InsertHfjl()
        {
            string jlxh = Request.QueryString["id"].ToString();
            using (DataClassesDataContext lqdb = new DataClassesDataContext())
            {
                var result = lqdb.Hfjl.FirstOrDefault(r => r.wtjlbh == jlxh);
                if (result != null)
                {
                    result.sm = txtsm.Text;
                    //result.fssj = Convert.ToDateTime(lblhfsj.Text);
                    result.fssj = DateTime.Now; //回访时间
                    result.czr = lblczr.Text;
                    result.wtjlbh = jlxh;
                    lqdb.SubmitChanges();
                }
                else
                {
                    Hfjl s = new Hfjl();
                    s.sm = txtsm.Text;
                    s.fssj = Convert.ToDateTime(lblhfsj.Text);
                    s.czr = lblczr.Text;
                    s.wtjlbh = jlxh;
                    lqdb.Hfjl.InsertOnSubmit(s);
                    lqdb.SubmitChanges();
                }


            }
        }
        //回访成功失败操作
        void wtCommend(char wtflag, char ishf)
        {
            string jlxh = Request.QueryString["id"].ToString();
            string wtname = txtwtname.Text;     //委托人姓名
            string bwtname = txtbwtname.Text;   //被委托人姓名
            string wtidcard = txtwtsf.Text;     //委托人身份证号码
            string bwtidcard = txtbwtsf.Text;   //被委托人身份证号码

            using (DataClassesDataContext lqDB = new DataClassesDataContext())
            {
                Wtjl w = lqDB.Wtjl.FirstOrDefault(r => r.jlbh == txtjlbh.Text);
                w.wtname = wtname;
                w.wtrsfzh = wtidcard;
                w.bwtname = bwtname;
                w.bwtrsfzh = bwtidcard;
                w.flag = wtflag;
                w.ishf = ishf;              //标记为已回访
                lqDB.SubmitChanges();
            }
        }
        //回访成功
        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!txtsm.Text.Equals(""))
            {
                //插入回访记录
                using (DataClassesDataContext lqDB = new DataClassesDataContext())
                {
                    Wtjl w = lqDB.Wtjl.FirstOrDefault(r => r.jlbh == txtjlbh.Text);
                    w.ishf = '9';               //标记为回访成功
                    lqDB.SubmitChanges();
                }
                InsertHfjl();
                Response.Write("<script language='javascript'>alert('回访成功！');window.close()</script> } ");
            }
            else
            {
                Frame.Global.GlobalFunctions.Alert("请输入回访工作记录！");
            }

        }
    }
}